//
//  PhotoViewerCell.swift
//  Weibo
//
//  Created by 杨雨哲 on 15/7/5.
//  Copyright © 2015年 yyz. All rights reserved.
//

import UIKit
import SDWebImage
import FFAutoLayout

protocol PhotoViewerCellDelegate: NSObjectProtocol {
    func photoViewerCellDidTapImage()
}

class PhotoViewerCell: UICollectionViewCell, UIScrollViewDelegate {
    
    weak var photoDelegate: PhotoViewerCellDelegate?
    
    var imageURL : NSURL? {
        didSet{
            resetScrollView()
            
            indicator.startAnimating()
            
            imageView.sd_setImageWithURL(imageURL, placeholderImage: nil) { (image, error, _, _) -> Void in
                self.indicator.stopAnimating()
                self.imagePosition(image)
            }
        }
    }
    
    private func resetScrollView(){
        scrollView.contentInset = UIEdgeInsetsZero
        scrollView.contentOffset = CGPointZero
        scrollView.contentSize = CGSizeZero
    }
    
    // MARK: - UIScrollViewDelegate 
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageView
    }
    
    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
        var offsetX = (scrollView.bounds.width - view!.frame.width) * 0.5
        if offsetX < 0 {
            offsetX = 0
        }
        var offsetY = (scrollView.bounds.height - view!.frame.height) * 0.5
        if offsetY < 0 {
            offsetY = 0
        }
        // 重新设置边距
        scrollView.contentInset = UIEdgeInsets(top: offsetY, left: offsetX, bottom: 0, right: 0)
    }
    
    private func imagePosition(image: UIImage) {
        let size = displaySize(image)
        
        if bounds.height < size.height {
            imageView.frame = CGRect(origin: CGPointZero, size: size)
            scrollView.contentSize = size
        }else {
            let y = (bounds.height - size.height) * 0.5
            imageView.frame = CGRect(origin: CGPointZero, size: size)
            scrollView.contentInset = UIEdgeInsetsMake(y, 0, 0, 0)
        }
    }
    
    private func displaySize(image: UIImage) -> CGSize{
        let scale = image.size.height/image.size.width
        let h = bounds.width * scale
        return CGSize(width: bounds.width, height: h)
    }
    
    func clickImage() {
        photoDelegate?.photoViewerCellDidTapImage()
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        scrollView.frame = bounds
        addSubview(scrollView)
        scrollView.delegate = self
        scrollView.maximumZoomScale = 2.0
        scrollView.minimumZoomScale = 0.5
        scrollView.addSubview(imageView)
        
        let tap = UITapGestureRecognizer(target: self, action: "clickImage")
        imageView.addGestureRecognizer(tap)
        imageView.userInteractionEnabled = true
        
        addSubview(indicator)
        indicator.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil)
        
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 懒加载控件
    lazy var imageView = UIImageView()
    lazy private var scrollView = UIScrollView()
    lazy private var indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
    
}
